package mao.thinkinjava.concurency.exchanger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Totoro
 * @date 2020/6/2
 * @explain:Exchanger<> 可以看成是两个篮子，一个装东西，一个取东西，然后交换。
 */
public class ExchangeDemo {
    private static int size = 10;

    public static void main(String[] args) {
        List<BigObject> producerHolder = new ArrayList<>(),
        consumerHolder = new ArrayList<>();
        Exchanger<List<BigObject>> ex = new Exchanger<>();

        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(new ExchangeProducer<>(new BigObjectGenerator(), ex, producerHolder,size));
        exec.execute(new ExchangeConsumer<>(ex, consumerHolder));
        try{
            TimeUnit.SECONDS.sleep(2);
            exec.shutdownNow();
        }catch (InterruptedException e){
        }
    }
}